#ifndef __ROM_FUNCTION2_PROCESS_C_
#define __ROM_FUNCTION2_PROCESS_C_

#include "udf_global.h"

#if ( UDF03_ROM_FUNCTION2 == 1 )

/*注: udsf_rom_function2_handle(UDF03内调用)为rom内置功能2简要复刻，与UDF13.c实现的2k参数依赖及功能完全一致*/
static void udsf_rom_function2_handle(void) __attribute__((unused));

#include "udf13_process.c"
#include "udf08_process.c"
#include "udf07_process.c"
#include "../udsf/udsf_cfar.c"
#include "../udsf/udsf_udf_switch.c"

static void udsf_rom_function2_handle(void)
{
	while(1){
		
		WDT->STR = 0x1;     //FEED DOG
		
		udsf_udf13_handle();
		udsf_udf08_handle();

		if(paramALG->algEn&ALG1_EN_MASK) P2_FFT_HANDLE(&paramALG->alg1);
		if(paramALG->algEn&ALG2_EN_MASK) P2_FFT_HANDLE(&paramALG->alg2);
		if(paramALG->algEn&ALG3_EN_MASK) P2_FFT_HANDLE(&paramALG->alg3);
		if(paramALG->algEn&ALG4_EN_MASK) P2_FFT_HANDLE(&paramALG->alg4);
		if(paramALG->algEn&ALG5_EN_MASK) P2_FFT_HANDLE(&paramALG->alg5);
		if(paramALG->algEn&ALG6_EN_MASK) P2_FFT_HANDLE(&paramALG->alg6);
		if(paramALG->algEn&ALG7_EN_MASK) P2_FFT_HANDLE(&paramALG->alg7);
		if(paramALG->algEn&ALG8_EN_MASK) P2_FFT_HANDLE(&paramALG->alg8);
		if(paramALG->algEn&ALG9_EN_MASK) P2_FFT_HANDLE(&paramALG->alg9);
		

		STRUCT_POINTRST *frameRst = (STRUCT_POINTRST *)(paramALG->framePackAddr);
		
		{
			int cfarRstNum1=0,cfarRstNum2=0;	
			BBE_CFARRST_T *cfar2Rst,*cfar1Rst;	

			uint32_t *powAbs=(uint32_t *)paramALG->cfarAbsAddr;
			_Bool *hitAFlag = (_Bool*)UDFDEF_ADDR_TEMP_256;
			_Bool *hitBFlag  = (_Bool*)(UDFDEF_ADDR_TEMP_256+128);
			
			if(paramALG->algEn&ALG10_EN_MASK) {
				cfarRstNum1=CFAR_HANDLE(&paramALG->alg10_cfar1);
				cfar1Rst=(BBE_CFARRST_T *)(BBE_ABUF_BASE+(bbe_regb_str->CFAR_CFG8&0xFFFF));
			}
			
			if(paramALG->algEn&ALG11_EN_MASK){ 
				cfarRstNum2=cfar_handle(&paramALG->alg11_cfar2);
				cfar2Rst=(BBE_CFARRST_T *)(BBE_ABUF_BASE+(bbe_regb_str->CFAR_CFG8&0xFFFF));
			}
			
			if((paramALG->algEn&ALG10_EN_MASK)&&(paramALG->algEn&ALG11_EN_MASK)){  //cfar1+cfar2
				CMPHIT_CFAR_TARGET(cfarRstNum1,cfar1Rst,cfarRstNum2,cfar2Rst,hitAFlag,hitBFlag);
				udsf_cfarframe(0,UDFDEF_RANGE_USE,frameRst,cfar1Rst,cfarRstNum1,powAbs,hitAFlag,CFAR12_EN);
			}else if(((paramALG->algEn&ALG10_EN_MASK)==0)&&(paramALG->algEn&ALG11_EN_MASK)){ //only cfar2
				udsf_cfarframe(0,UDFDEF_RANGE_USE,frameRst,cfar2Rst,cfarRstNum2,powAbs,hitAFlag,CFAR2_EN);
			}else if(((paramALG->algEn&ALG11_EN_MASK)==0)&&(paramALG->algEn&ALG10_EN_MASK)){                                                                   //only cfar1
				udsf_cfarframe(0,UDFDEF_RANGE_USE,frameRst,cfar1Rst,cfarRstNum1,powAbs,hitAFlag,CFAR1_EN);
			}else{
				frameRst->targetNum = 0;
			}
		}
		
		
		if(paramALG->algEn&ALG12_EN_MASK)	FFT3D_PRE_HANDLE(&paramALG->alg12,frameRst);
		
		if(paramALG->algEn&ALG13_EN_MASK) P2_FFT_HANDLE(&paramALG->alg13);
		if(paramALG->algEn&ALG14_EN_MASK) P2_FFT_HANDLE(&paramALG->alg14);
		if(paramALG->algEn&ALG15_EN_MASK) P2_FFT_HANDLE(&paramALG->alg15);
		if(paramALG->algEn&ALG16_EN_MASK) P2_FFT_HANDLE(&paramALG->alg16);
		if(paramALG->algEn&ALG17_EN_MASK) P2_FFT_HANDLE(&paramALG->alg17);
		if(paramALG->algEn&ALG18_EN_MASK) P2_FFT_HANDLE(&paramALG->alg18);
		if(paramALG->algEn&ALG19_EN_MASK) P2_FFT_HANDLE(&paramALG->alg19);		
		
		if(paramALG->algEn&ALG20_EN_MASK)	FFT3D_PRE_HANDLE(&paramALG->alg20,frameRst);
		
		if(paramALG->algEn&ALG21_EN_MASK) P2_FFT_HANDLE(&paramALG->alg21);
		if(paramALG->algEn&ALG22_EN_MASK) P2_FFT_HANDLE(&paramALG->alg22);
		
		if(paramALG->algEn&ALG23_EN_MASK){
			bbe_regb_str->P2_CFG0=paramALG->alg23.p2Cfg0;
			bbe_regb_str->P2_CFG1=paramALG->alg23.p2Cfg1;
			bbe_regb_str->P2_CFG2=paramALG->alg23.p2Cfg2;
			bbe_regb_str->P2_CFG3=paramALG->alg23.p2Cfg3;
			bbe_regb_str->P2_CFG4=paramALG->alg23.p2Cfg4;
			bbe_regb_str->P2_CFG5=paramALG->alg23.p2Cfg5;

			bbe_regb_str->P2_CFG7=paramALG->alg23.p2Cfg7;
			bbe_regb_str->P2_CFG8=paramALG->alg23.p2Cfg8;
			BBE_ISR_CLEAR(BBE_ISR_P2_JUMP_END);
			BBE_OPGATE_EN(BBE_GATE_P2);
			
			for(uint32_t cnt=0;cnt<frameRst->targetNum;cnt++){
				bbe_regb_str->P2_CFG6=paramALG->alg23.p2Cfg6+cnt*ANT_MAX_NUM*NUM_BYTE32;
				BBE_OPTRIG(BBE_TRIG_P2);
				BBE_WAITISR(BBE_ISR_P2_JUMP_END);

				frameRst->target[cnt].d3Idx = BBE_P2_STA0_MAXJUMPIDX;
			}
			BBE_OPGATE_DIS(BBE_GATE_P2);	
		}
		
		if(paramALG->algEn&ALG24_EN_MASK) P2_FFT_HANDLE(&paramALG->alg24);
		
		udsf_udf07_handle();
		break;
	}
}
#endif

#endif

